{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pandas中的时间序列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python中datetime的复习<br>\n",
    "date_range<br>\n",
    "set_index<br>\n",
    "resample<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## time datetime timedelta复习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "timedelta表示时间间隔"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1534237517.760913"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.time()       # 获得现在距离1970年初的时间戳"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2018, tm_mon=8, tm_mday=12, tm_hour=17, tm_min=31, tm_sec=1, tm_wday=6, tm_yday=224, tm_isdst=0)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime(1534066261)           # 将时间戳转为易读的时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2018, tm_mon=8, tm_mday=14, tm_hour=17, tm_min=5, tm_sec=17, tm_wday=1, tm_yday=226, tm_isdst=0)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime(time.time())             "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2018-08-14 17:05:17'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))        # strftime() 可以将时间转化为指定的格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2018-08-14'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.strftime('%Y-%m-%d', time.localtime(time.time()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'17:05:17'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.strftime('%H:%M:%S', time.localtime(time.time()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2018, tm_mon=8, tm_mday=12, tm_hour=17, tm_min=34, tm_sec=4, tm_wday=6, tm_yday=224, tm_isdst=-1)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "local = time.strptime('2018-08-12 17:34:04', '%Y-%m-%d %H:%M:%S')\n",
    "local"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1534066444.0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.mktime(local)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2018, tm_mon=8, tm_mday=12, tm_hour=17, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=224, tm_isdst=-1)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "local2 = time.strptime('2018-08-12 17', '%Y-%m-%d %H')\n",
    "local2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1534064400.0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.mktime(local2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2018, 8, 14, 17, 5, 17, 885396)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now = datetime.now()\n",
    "now"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2018, 8, 14)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.year, now.month, now.day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2018-08-14 17:05:17'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.strftime('%Y-%m-%d %H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2018-08-14'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.strftime('%Y-%m-%d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'17:05:17'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.strftime('%H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2018, 8, 12, 17, 39, 50)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.strptime('2018-08-12 17:39:50', '%Y-%m-%d %H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2018, 8, 12, 0, 0)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime(2018, 8, 12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1534237517.885396"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.timestamp()         # 转化为时间戳"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2018, 8, 12, 17, 39, 50, 321240)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.fromtimestamp(1534066790.32124)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. timedelta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.timedelta(days=476, seconds=61517, microseconds=973284)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta = datetime.now() - datetime(2017, 4, 25)\n",
    "delta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "476"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta.days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "61517"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta.seconds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "973284"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta.microseconds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## date_range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-08-12', '2018-08-13', '2018-08-14', '2018-08-15',\n",
       "               '2018-08-16', '2018-08-17', '2018-08-18', '2018-08-19',\n",
       "               '2018-08-20', '2018-08-21', '2018-08-22', '2018-08-23',\n",
       "               '2018-08-24', '2018-08-25', '2018-08-26', '2018-08-27',\n",
       "               '2018-08-28', '2018-08-29', '2018-08-30', '2018-08-31',\n",
       "               '2018-09-01', '2018-09-02', '2018-09-03', '2018-09-04',\n",
       "               '2018-09-05', '2018-09-06', '2018-09-07', '2018-09-08',\n",
       "               '2018-09-09', '2018-09-10', '2018-09-11', '2018-09-12',\n",
       "               '2018-09-13', '2018-09-14', '2018-09-15', '2018-09-16',\n",
       "               '2018-09-17', '2018-09-18', '2018-09-19', '2018-09-20',\n",
       "               '2018-09-21', '2018-09-22', '2018-09-23', '2018-09-24',\n",
       "               '2018-09-25'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2018-08-12', '2018-09-25')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-08-12', '2018-08-19', '2018-08-26', '2018-09-02',\n",
       "               '2018-09-09', '2018-09-16', '2018-09-23'],\n",
       "              dtype='datetime64[ns]', freq='W-SUN')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2018-08-12', '2018-09-25', freq='W')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-08-31'], dtype='datetime64[ns]', freq='M')"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2018-08-12', '2018-09-25', freq='M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex([], dtype='datetime64[ns]', freq='Q-DEC')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2018-08-12', '2018-09-25', freq='Q')         # D-天  W-周  M-月  Q-季度  A-年  H-小时  T-分  S-秒"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-08-12', '2018-08-19', '2018-08-26', '2018-09-02',\n",
       "               '2018-09-09', '2018-09-16', '2018-09-23', '2018-09-30',\n",
       "               '2018-10-07', '2018-10-14'],\n",
       "              dtype='datetime64[ns]', freq='W-SUN')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2018-08-12', freq='W', periods=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    'time': pd.date_range('2018-08-12', freq='T', periods=200000),\n",
    "    'cpu': np.random.randn(200000) + 10\n",
    "}\n",
    "df = pd.DataFrame(data, columns=['time', 'cpu'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-08-12 00:00:00</td>\n",
       "      <td>9.626151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-08-12 00:01:00</td>\n",
       "      <td>9.917640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-08-12 00:02:00</td>\n",
       "      <td>10.138593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-08-12 00:03:00</td>\n",
       "      <td>10.786513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-08-12 00:04:00</td>\n",
       "      <td>8.950595</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 time        cpu\n",
       "0 2018-08-12 00:00:00   9.626151\n",
       "1 2018-08-12 00:01:00   9.917640\n",
       "2 2018-08-12 00:02:00  10.138593\n",
       "3 2018-08-12 00:03:00  10.786513\n",
       "4 2018-08-12 00:04:00   8.950595"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>199995</th>\n",
       "      <td>2018-12-28 21:15:00</td>\n",
       "      <td>10.657856</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199996</th>\n",
       "      <td>2018-12-28 21:16:00</td>\n",
       "      <td>12.131958</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199997</th>\n",
       "      <td>2018-12-28 21:17:00</td>\n",
       "      <td>10.368931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199998</th>\n",
       "      <td>2018-12-28 21:18:00</td>\n",
       "      <td>8.781899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199999</th>\n",
       "      <td>2018-12-28 21:19:00</td>\n",
       "      <td>10.103600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      time        cpu\n",
       "199995 2018-12-28 21:15:00  10.657856\n",
       "199996 2018-12-28 21:16:00  12.131958\n",
       "199997 2018-12-28 21:17:00  10.368931\n",
       "199998 2018-12-28 21:18:00   8.781899\n",
       "199999 2018-12-28 21:19:00  10.103600"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>480</th>\n",
       "      <td>2018-08-12 08:00:00</td>\n",
       "      <td>8.984850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>481</th>\n",
       "      <td>2018-08-12 08:01:00</td>\n",
       "      <td>8.927575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>482</th>\n",
       "      <td>2018-08-12 08:02:00</td>\n",
       "      <td>11.048370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>483</th>\n",
       "      <td>2018-08-12 08:03:00</td>\n",
       "      <td>10.352017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>484</th>\n",
       "      <td>2018-08-12 08:04:00</td>\n",
       "      <td>9.881482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>485</th>\n",
       "      <td>2018-08-12 08:05:00</td>\n",
       "      <td>8.936678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>486</th>\n",
       "      <td>2018-08-12 08:06:00</td>\n",
       "      <td>9.411199</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>487</th>\n",
       "      <td>2018-08-12 08:07:00</td>\n",
       "      <td>10.781394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>488</th>\n",
       "      <td>2018-08-12 08:08:00</td>\n",
       "      <td>10.523912</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>489</th>\n",
       "      <td>2018-08-12 08:09:00</td>\n",
       "      <td>9.546083</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>490</th>\n",
       "      <td>2018-08-12 08:10:00</td>\n",
       "      <td>9.390208</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   time        cpu\n",
       "480 2018-08-12 08:00:00   8.984850\n",
       "481 2018-08-12 08:01:00   8.927575\n",
       "482 2018-08-12 08:02:00  11.048370\n",
       "483 2018-08-12 08:03:00  10.352017\n",
       "484 2018-08-12 08:04:00   9.881482\n",
       "485 2018-08-12 08:05:00   8.936678\n",
       "486 2018-08-12 08:06:00   9.411199\n",
       "487 2018-08-12 08:07:00  10.781394\n",
       "488 2018-08-12 08:08:00  10.523912\n",
       "489 2018-08-12 08:09:00   9.546083\n",
       "490 2018-08-12 08:10:00   9.390208"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[(df.time>='2018-08-12 08:00:00') & (df.time<='2018-08-12 08:10:00')]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## set_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:00:00</th>\n",
       "      <td>9.626151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:01:00</th>\n",
       "      <td>9.917640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:02:00</th>\n",
       "      <td>10.138593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:03:00</th>\n",
       "      <td>10.786513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:04:00</th>\n",
       "      <td>8.950595</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           cpu\n",
       "time                          \n",
       "2018-08-12 00:00:00   9.626151\n",
       "2018-08-12 00:01:00   9.917640\n",
       "2018-08-12 00:02:00  10.138593\n",
       "2018-08-12 00:03:00  10.786513\n",
       "2018-08-12 00:04:00   8.950595"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.set_index(\"time\")\n",
    "df2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "set_index()等效于下面的代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = pd.to_datetime(df.time)\n",
    "s.head()\n",
    "df.index = s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:00:00</th>\n",
       "      <td>2018-08-12 00:00:00</td>\n",
       "      <td>9.626151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:01:00</th>\n",
       "      <td>2018-08-12 00:01:00</td>\n",
       "      <td>9.917640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:02:00</th>\n",
       "      <td>2018-08-12 00:02:00</td>\n",
       "      <td>10.138593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:03:00</th>\n",
       "      <td>2018-08-12 00:03:00</td>\n",
       "      <td>10.786513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:04:00</th>\n",
       "      <td>2018-08-12 00:04:00</td>\n",
       "      <td>8.950595</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   time        cpu\n",
       "time                                              \n",
       "2018-08-12 00:00:00 2018-08-12 00:00:00   9.626151\n",
       "2018-08-12 00:01:00 2018-08-12 00:01:00   9.917640\n",
       "2018-08-12 00:02:00 2018-08-12 00:02:00  10.138593\n",
       "2018-08-12 00:03:00 2018-08-12 00:03:00  10.786513\n",
       "2018-08-12 00:04:00 2018-08-12 00:04:00   8.950595"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:00:00</th>\n",
       "      <td>9.626151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:01:00</th>\n",
       "      <td>9.917640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:02:00</th>\n",
       "      <td>10.138593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:03:00</th>\n",
       "      <td>10.786513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:04:00</th>\n",
       "      <td>8.950595</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           cpu\n",
       "time                          \n",
       "2018-08-12 00:00:00   9.626151\n",
       "2018-08-12 00:01:00   9.917640\n",
       "2018-08-12 00:02:00  10.138593\n",
       "2018-08-12 00:03:00  10.786513\n",
       "2018-08-12 00:04:00   8.950595"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.drop('time', axis=1)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-08-12 08:00:00</th>\n",
       "      <td>8.984850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 08:01:00</th>\n",
       "      <td>8.927575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 08:02:00</th>\n",
       "      <td>11.048370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 08:03:00</th>\n",
       "      <td>10.352017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 08:04:00</th>\n",
       "      <td>9.881482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 08:05:00</th>\n",
       "      <td>8.936678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 08:06:00</th>\n",
       "      <td>9.411199</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 08:07:00</th>\n",
       "      <td>10.781394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 08:08:00</th>\n",
       "      <td>10.523912</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 08:09:00</th>\n",
       "      <td>9.546083</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 08:10:00</th>\n",
       "      <td>9.390208</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           cpu\n",
       "time                          \n",
       "2018-08-12 08:00:00   8.984850\n",
       "2018-08-12 08:01:00   8.927575\n",
       "2018-08-12 08:02:00  11.048370\n",
       "2018-08-12 08:03:00  10.352017\n",
       "2018-08-12 08:04:00   9.881482\n",
       "2018-08-12 08:05:00   8.936678\n",
       "2018-08-12 08:06:00   9.411199\n",
       "2018-08-12 08:07:00  10.781394\n",
       "2018-08-12 08:08:00  10.523912\n",
       "2018-08-12 08:09:00   9.546083\n",
       "2018-08-12 08:10:00   9.390208"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['2018-08-12 08:00:00':'2018-08-12 08:10:00']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:00:00</th>\n",
       "      <td>9.626151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:01:00</th>\n",
       "      <td>9.917640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:02:00</th>\n",
       "      <td>10.138593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:03:00</th>\n",
       "      <td>10.786513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:04:00</th>\n",
       "      <td>8.950595</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           cpu\n",
       "time                          \n",
       "2018-08-12 00:00:00   9.626151\n",
       "2018-08-12 00:01:00   9.917640\n",
       "2018-08-12 00:02:00  10.138593\n",
       "2018-08-12 00:03:00  10.786513\n",
       "2018-08-12 00:04:00   8.950595"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df['2018-08-12']\n",
    "df2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-08-12 23:55:00</th>\n",
       "      <td>8.192220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 23:56:00</th>\n",
       "      <td>11.100015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 23:57:00</th>\n",
       "      <td>10.252504</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 23:58:00</th>\n",
       "      <td>10.293859</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 23:59:00</th>\n",
       "      <td>9.666420</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           cpu\n",
       "time                          \n",
       "2018-08-12 23:55:00   8.192220\n",
       "2018-08-12 23:56:00  11.100015\n",
       "2018-08-12 23:57:00  10.252504\n",
       "2018-08-12 23:58:00  10.293859\n",
       "2018-08-12 23:59:00   9.666420"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                  cpu\n",
      "2018-08-12   9.982413\n",
      "2018-08-13   9.991605\n",
      "2018-08-14  10.024788\n",
      "2018-08-15  10.042854\n",
      "2018-08-16  10.021630\n",
      "                  cpu\n",
      "2018-12-24   9.971736\n",
      "2018-12-25  10.033450\n",
      "2018-12-26  10.003885\n",
      "2018-12-27  10.003722\n",
      "2018-12-28   9.987678\n"
     ]
    }
   ],
   "source": [
    "df2 = df.groupby(df.index.date).mean()\n",
    "print(df2.head())\n",
    "print(df2.tail())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            cpu\n",
      "time           \n",
      "0     10.007725\n",
      "1      9.996076\n",
      "2      9.984526\n",
      "3     10.017543\n",
      "4      9.990612\n",
      "            cpu\n",
      "time           \n",
      "19    10.002742\n",
      "20     9.974482\n",
      "21     9.972723\n",
      "22    10.006801\n",
      "23     9.997885\n"
     ]
    }
   ],
   "source": [
    "df2 = df.groupby(df.index.hour).mean()\n",
    "print(df2.head())\n",
    "print(df2.tail())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            cpu\n",
      "time           \n",
      "32     9.982413\n",
      "33    10.014457\n",
      "34     9.977128\n",
      "35     9.993188\n",
      "36     9.994639\n",
      "            cpu\n",
      "time           \n",
      "48    10.004162\n",
      "49     9.989295\n",
      "50    10.005922\n",
      "51     9.986940\n",
      "52    10.000376\n"
     ]
    }
   ],
   "source": [
    "df2 = df.groupby(df.index.week).mean()\n",
    "print(df2.head())\n",
    "print(df2.tail())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## resample"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2 = df.resample('90S').mean()           # 重新取样，时间间隔为90秒了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:00:00</th>\n",
       "      <td>9.771895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:01:30</th>\n",
       "      <td>10.138593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:03:00</th>\n",
       "      <td>9.868554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:04:30</th>\n",
       "      <td>9.882623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:06:00</th>\n",
       "      <td>10.737730</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           cpu\n",
       "time                          \n",
       "2018-08-12 00:00:00   9.771895\n",
       "2018-08-12 00:01:30  10.138593\n",
       "2018-08-12 00:03:00   9.868554\n",
       "2018-08-12 00:04:30   9.882623\n",
       "2018-08-12 00:06:00  10.737730"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:00:00</th>\n",
       "      <td>10.786513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:05:00</th>\n",
       "      <td>12.005524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:10:00</th>\n",
       "      <td>11.940386</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:15:00</th>\n",
       "      <td>10.571349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-08-12 00:20:00</th>\n",
       "      <td>12.368275</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           cpu\n",
       "time                          \n",
       "2018-08-12 00:00:00  10.786513\n",
       "2018-08-12 00:05:00  12.005524\n",
       "2018-08-12 00:10:00  11.940386\n",
       "2018-08-12 00:15:00  10.571349\n",
       "2018-08-12 00:20:00  12.368275"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.resample('5T').max()\n",
    "df2.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
